package com.strawcom.charmring.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.annotation.SuppressLint;

/**
 * @author WangPeng
 */
@SuppressLint("SimpleDateFormat")
public class FormatUtil {

	public static final String HH_mm = "HH:mm";
	public static final String yyyy_MM_dd = "yyyy-MM-dd";
	public static final String yyyyMMdd = "yyyyMMdd";
	public static final String HHmmss = "HHmmss";
	public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";

	public static final String dd = "dd";
	public static final String E = "E"; // 星期
	public static final String MM_dd_ = "MM-dd";//"MM\u6708dd\u65e5"; // 几月几日

	private static final SimpleDateFormat sdf = new SimpleDateFormat();;

	public static Date parseDate(String date, String pattern) {
		Date result = null;
		try {
			sdf.applyPattern(pattern);
			result = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String formatDate(Date date, String pattern) {
		sdf.applyPattern(pattern);
		return sdf.format(date);
	}

	public static String formatDate(String string, String pattern1, String pattern2) {
		String result = null;
		try {
			sdf.applyPattern(pattern1);
			Date date = sdf.parse(string);
			sdf.applyPattern(pattern2);
			result = sdf.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Deprecated public static int calcIndexOfWeek(String date, String pattern) {
		boolean deprecated = true;
		if (deprecated) return calcIdxOfWeek(date, pattern);

		final String[] weeks = new String[] { "\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", };
		String result = formatDate(date, pattern, E);
		for (int i = 0; i < weeks.length; i++) {
			if (result.contains(weeks[i])) {
				return i;
			}
		}
		return -1;
	}

	public static int calcIdxOfWeek(String date, String pattern) {
		Calendar cc = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
		cc.setTime(parseDate(date, pattern));
		int weekday = cc.get(Calendar.DAY_OF_WEEK); // 以这个为准：西方一周的第一天是周日
		return weekday;
	}

	/** 返回间隔天数 */
	public static int timeSpcaing(String date1, String date2, String pattern) {
		Date d1 = parseDate(date1, pattern);
		Date d2 = parseDate(date2, pattern);
		return (int) (Math.abs((d1.getTime() - d2.getTime())) / (24 * 3600000));
	}

	/** 返回间隔天数 */
	public static int timeSpcaing(Date date1, Date date2) {
		return (int) (Math.abs((date1.getTime() - date2.getTime())) / (24 * 3600000));
	}

	/** 今天剩余秒数 */
	public static long todaySurplusSeconds() {
		final Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		final long diff = cal.getTimeInMillis() - System.currentTimeMillis();
		return diff / 1000;
	}

	/** 今天剩余分钟 */
	public static long todaySurplusMinutes() {
		return todaySurplusSeconds() / 60;
	}

}
